package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysProvince;
import com.mars.module.admin.entity.SysScenicSpot;
import com.mars.module.admin.mapper.SysProvinceMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysScenicAreaRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysScenicAreaMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysScenicArea;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysScenicAreaService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 景区业务层处理
 *
 * @author mars
 * @date 2024-03-25
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysScenicAreaServiceImpl implements ISysScenicAreaService {

    private final SysScenicAreaMapper baseMapper;
    private final SysProvinceMapper provinceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysScenicArea add(SysScenicAreaRequest request) {
        SysScenicArea entity = SysScenicArea.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysScenicAreaRequest request) {
        SysScenicArea entity = SysScenicArea.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysScenicArea getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysScenicArea> pageList(SysScenicAreaRequest request) {
        Page<SysScenicArea> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysScenicArea> query = this.buildWrapper(request);
        IPage<SysScenicArea> pageRecord = baseMapper.selectPage(page, query);
        List<SysScenicArea> records = pageRecord.getRecords();
        List<SysScenicArea> recordsNew = new ArrayList<>();
        for (SysScenicArea area : records) {
            SysProvince sysProvince = provinceMapper.selectById(area.getProvinceId());
            area.setProvinceName(sysProvince == null ? "" : sysProvince.getName());
            recordsNew.add(area);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysScenicArea> list(SysScenicAreaRequest request) {
        LambdaQueryWrapper<SysScenicArea> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysScenicArea> buildWrapper(SysScenicAreaRequest param) {
        LambdaQueryWrapper<SysScenicArea> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())) {
            query.like(SysScenicArea::getName, param.getName());
        }
        if (param.getProvinceId() != null) {
            query.like(SysScenicArea::getProvinceId, param.getProvinceId());
        }
        if (StringUtils.isNotBlank(param.getIntroduce())) {
            query.like(SysScenicArea::getIntroduce, param.getIntroduce());
        }
        if (StringUtils.isNotBlank(param.getPhone())) {
            query.like(SysScenicArea::getPhone, param.getPhone());
        }
        if (StringUtils.isNotBlank(param.getAddress())) {
            query.like(SysScenicArea::getAddress, param.getAddress());
        }
        if (StringUtils.isNotBlank(param.getPicture())) {
            query.like(SysScenicArea::getPicture, param.getPicture());
        }
        return query;
    }

}
